package com.orange.lswidget.widgets.event.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.CallLog.Calls;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;

import com.orange.lswidget.R;
import com.orange.lswidget.widgets.event.dao.MissCallDao;
import com.orange.notificationsummary.event.pil.defaultimpl.BaseEvent;
import com.orange.notificationsummary.utils.Constants;
import com.orange.notificationsummary.utils.Constants.EventType;
import com.orange.notificationsummary.utils.EventLSWidgetUtils;
import com.orange.util.LSEventUtils;
import com.orange.util.LSServiceUtils;
import com.orange.util.LogHelper;
import com.orange.util.MissCallUtils;

public class MissCallManager extends EventManager implements EventType {
	private static final String			TAG		= "MissCallManager";

	private MissCallDao					missCallDao;
	private List<Map<String, String>>	mResultList;
	private static MissCallManager		sMissCallManager;
	private static Object				sObject	= new Object();
	private String						mID;

	private MissCallManager(Context context) {
		super(context);
		missCallDao = new MissCallDao(context);
		mResultList = new ArrayList<Map<String, String>>();

		mBasicIconResId = R.drawable.event_missed_call_icon;
		mDetailedIconResId = R.drawable.event_missed_call_icon_small;
		mBasicTypeString = context.getResources()
				.getString(R.string.missedCall);
		mDetailedTypeString = context.getResources().getString(
				R.string.missedCall);

		mType = MISSCALL_UPDATE;
	}

	public static MissCallManager getInstance(Context context) {
		if (sMissCallManager == null) {
			synchronized (sObject) {
				if (sMissCallManager == null) {
					sMissCallManager = new MissCallManager(context);
				}
			}
		}
		return sMissCallManager;
	}

	public List<Map<String, String>> queryAllMissCallToList() {
		if (!mResultList.isEmpty()) {
			mResultList.clear();
		}
		mResultList = missCallDao.queryAllMissCallToList();

		setCount();
		return mResultList;
	}

	@Override
	protected void setCount() {
		mCount = mResultList.size();
	}

	@Override
	public void updateDetailedInfo() {
		LogHelper.i(TAG, "updateDetailedInfo. ");
		BaseEvent missCall = missCallDao.queryMissCallEvent();

		if (null != missCall) {
			mIsVip = missCall.isVip;
			mNumberCountByContact = missCall.phoneNumberCount;
			mBitmap = missCall.contacts_image;
			mID = String.valueOf(missCall.id);
			mLastEventTime = missCall.event_date;
			mTime = EventLSWidgetUtils.dateFormat(mContext, mLastEventTime);

			List<String> numberList = MissCallUtils.getNumbersByResult(
					mContext, mResultList);
			LSEventUtils.updateNamesByNumbers(mContext, numberList, mNames);

			if (!EventLSWidgetUtils.getEventPreviewSetting(mContext)) {
				mNumberLabel = missCall.evnet_number_type;
			}
			if (mNumberCountByContact > 1 && !TextUtils.isEmpty(mNumberLabel)) {
				mFrom = LSEventUtils.getNameString(mContext, mNames,
						mNumberLabel);
			} else {
				mFrom = LSEventUtils.getNameString(mContext, mNames);
			}
		}

		LogHelper.d(TAG, "updateDetailedInfo. mID: " + mID + " mTime: " + mTime
				+ " mFrom: " + mFrom + " ,label: " + mNumberLabel
				+ " ,numberCount: " + mNumberCountByContact + " ,mIsVip: "
				+ mIsVip);
	}

	@Override
	public void updateSummaryInfo() {
		LogHelper.v(TAG, "updateSummaryInfo. ");
		if (null == mResultList || mResultList.isEmpty()) {
			mLastEventTime = 0;
		} else {
			if (mCount > 1) {
				mBasicTypeString = mContext.getResources().getString(
						R.string.missedCalls);
			} else {
				mBasicTypeString = mContext.getResources().getString(
						R.string.missedCall);
				mDetailedTypeString = mContext.getResources().getString(
						R.string.missedCall);
			}

			mLastEventTime = Long.parseLong(mResultList.get(0).get(Calls.DATE));
			mTime = EventLSWidgetUtils.dateFormat(mContext, mLastEventTime);
			LogHelper.i(TAG, "updateSummaryInfo. mLastEventTime: "
					+ mLastEventTime);

			List<String> numberList = MissCallUtils.getNumbersByResult(
					mContext, mResultList);
			LSEventUtils.updateNamesByNumbers(mContext, numberList, mNames);
		}
	}

	@Override
	public RemoteViews getDetailedView(RemoteViews remoteViews) {
		remoteViews.setViewVisibility(R.id.basic, View.GONE);
		remoteViews.setViewVisibility(R.id.detailed, View.VISIBLE);

		remoteViews.setImageViewResource(R.id.event_detailed_icon,
				mDetailedIconResId);
		remoteViews.setTextViewText(R.id.event_detailed_type,
				mDetailedTypeString);
		remoteViews.setTextViewText(R.id.event_detailed_time, mTime);
		remoteViews.setTextViewText(R.id.event_detailed_number, mFrom);

		LogHelper.i(TAG, "getDetailedView. mTime: " + mTime + " ,mFrom: "
				+ mFrom);

		remoteViews.setViewVisibility(R.id.mms_attachement_icon, View.GONE);

		remoteViews.setViewVisibility(R.id.mms_image_framelayout, View.GONE);
		remoteViews.setViewVisibility(R.id.contact_image_framelayout,
				View.VISIBLE);

		// image
		if (null == mBitmap) {
			remoteViews.setImageViewResource(R.id.contact_detailed_image,
					R.drawable.event_nobody);
		} else {
			remoteViews
					.setImageViewBitmap(R.id.contact_detailed_image, mBitmap);
		}

		if (mIsVip) {
			remoteViews.setViewVisibility(R.id.contact_vip_image, View.VISIBLE);
		} else {
			remoteViews.setViewVisibility(R.id.contact_vip_image,
					View.INVISIBLE);
		}

		return remoteViews;
	}

	// if MissedCall
	public void startMissedCallActivity() throws Exception {
		Intent intent = new Intent();

		boolean isNtfcExists = EventLSWidgetUtils.isPackageExist(mContext,
				Constants.PKG_NOTIFICATION_SUMMARY);
		boolean isNtfcEnabled = EventLSWidgetUtils
				.isNotificationScreenEnabled(mContext);

		boolean isFromSame = MissCallUtils.isMissedCallFromSameSender(mContext,
				mResultList);
		LogHelper.d(TAG, "startMissedCallActivity. isNtfcExists: "
				+ isNtfcExists + " ,isNtfcEnabled: " + isNtfcEnabled
				+ " ,isFromSame: " + isFromSame);

		if (!isNtfcExists) {
			// startCallDetailActivity(intent, mID);
			startCallDetailActivity(intent);
		} else {
			if (!isNtfcEnabled) {
				startCallDetailActivity(intent);
			} else {
				if (isFromSame) {
					startCallDetailActivity(intent);
				} else {
					startDialtactsActivity(intent);
				}
			}
		}
	}

	private void startDialtactsActivity(Intent intent) {
		intent.setAction(Intent.ACTION_VIEW);
		intent.setType(Calls.CONTENT_TYPE);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		LSServiceUtils.startActivity(mContext, intent);
	}

	private void startCallDetailActivity(Intent intent) {
		Map<String, String> callMap = missCallDao.queryLastCallInfo();

		String lastNumber = callMap.get(Calls.NUMBER);
		String missCallId = callMap.get(Calls._ID);

		int ret = missCallDao.updateMissCallStatusByNumber(Calls.NUMBER,
				lastNumber);
		LogHelper.i(TAG, "startLastCallActivityByNtfc. lastNumber: "
				+ lastNumber + " missCallId: " + missCallId + " ,ret: " + ret);

		if (ret != 0) {
			MissCallUtils.cancelMissedCallsNotification(mContext);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setData(Uri.withAppendedPath(Calls.CONTENT_URI, missCallId));
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			LSServiceUtils.startActivity(mContext, intent);
		}
	}
}
